var rowCount;

/*Add row on bill table*/
function addRow() {

	var table = document.getElementById("bill");
	rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	
	// Generate Sr.No. row
	var cell1 = row.insertCell(0);
	cell1.innerHTML = rowCount;

	// Generate Product Code row
	var cell2 = row.insertCell(1);
	var prodCodeId="prodCode" + rowCount;
	var element1 = document.createElement("input");
	element1.type = "text";
	element1.name = "productCode" + rowCount;
	element1.id = prodCodeId;
	element1.setAttribute("class","billproddLable");
	cell2.appendChild(element1);

	// Generate Product Name row
	var cell3 = row.insertCell(2);
	var prodNameId="prodName" + rowCount;
	cell3.id = prodNameId;
	cell3.innerHTML = "";

	// Generate Product Quantity row
	var cell5 = row.insertCell(3);
	
	var rateId="rate" + rowCount;
	var amtId="amount" + rowCount;
	var element2=document.createElement("input");
	var quantityId="quantity" + rowCount;
	element2.setAttribute("type", "text");
	element2.setAttribute("name", "quantity" + rowCount);
	element2.setAttribute("id",quantityId);
	element2.setAttribute("class","billproddLable");
	element2.setAttribute("onfocus", "getProductInfo('"+prodCodeId+"','"+prodNameId+"','"+rateId+"')");
	element2.setAttribute("onkeyup","calculateAmount('"+quantityId+"','"+rateId+"','"+amtId+"')");
	cell5.appendChild(element2);

	// Generate Product Coupon Code row
	var cell6 = row.insertCell(4);
	var element3 = document.createElement("input");
	element3.type = "text";
	element3.name = "couponCode" + rowCount;
	element3.id = "couponCode" + rowCount;
	element3.setAttribute("class","billproddLable");
	cell6.appendChild(element3);
	
	// Generate Product Price row
	var cell7 = row.insertCell(5);
	cell7.id = rateId;
	cell7.innerHTML = "";

	// Generate Product Amount row
	var cell8 = row.insertCell(6);
	cell8.id =amtId; 
	cell8.innerHTML = "";
}

/*Delete row in bill table*/
function deleteRow() {
	var table = document.getElementById("bill");
	var rowCount = table.rows.length;
	var row=rowCount-1;
	
	if(row!=1){
		var prodTotal=document.getElementById("amount"+row).innerHTML;
		if(prodTotal==null || prodTotal=="")
			table.deleteRow(row);
		else{
			var isConfirm=confirm("Are You Sure to delete row?");
			if(isConfirm){
				table.deleteRow(row);
				getTotalamount();
			}	
		}
	}
}

/*Submit bill into database using bill controller*/
function generateBill() {
	var custName = document.getElementById("custName").value;
	if(custName==null || custName==""){
		alert("Customer Name is not entered.Please Put Custmor Name");
		return;
	}
	var custAddress = document.getElementById("custAddress").value;
	var custMobile = document.getElementById("custMobile").value;
	var custEmailAddress = document.getElementById("custEmailAddress").value;
	var invoiceNo = document.getElementById("invoiceNo").innerHTML;
	var showPayMode = document.getElementById("showPayMode").value;
	var refNo = document.getElementById("refNo").value;
	//var vatPer = document.getElementById("vatPer").innerHTML;
	
	if(showPayMode=='Select Pay Mode'){
		alert("Please Select Payment Mode");
		return;
	}
	var custProdInfo="[";
	
	var table = document.getElementById("bill");
	rowCount = table.rows.length;
	for ( var count = 1; count <rowCount; count++) {
		var prodCode = document.getElementById("prodCode"+count).value;
		var quantity = document.getElementById("quantity"+count).value;
		var prodPrice = document.getElementById("rate"+count).innerHTML;
		if(prodCode==null || prodCode==""){
			alert("Product Code is not entered.Please Put Product Code ");
			return;
		}else if(quantity==null || quantity==""){
			alert("Product Quantity is not entered.Please Put Product Quantity");
			return;
		}else if (isNaN(quantity)){
		    alert("Product Quantity is not Number.Please Put Product Quantity is Number");
		    return;
		}
		else{
			var prodInfo="{prodCode:C"+prodCode+",quantity:"+quantity+",prodPrice:"+prodPrice+"},";
			custProdInfo=custProdInfo+prodInfo;
		}
	}
	custProdInfo=custProdInfo+"]";
	var totalAmt = document.getElementById("prodTotal").innerHTML;
	var discount = document.getElementById("discount").value;	
	var courierCharges = document.getElementById("courierCharges").value;
	var grandTotalAmt = document.getElementById("grandTotal").innerHTML;
	var action="GB";
	jQuery.ajax({
        type:"post",
        url:"custBill",
        data:{
        	ACTION: action,
        	custName: custName,
        	custAddress: custAddress,
        	custMobile: custMobile,
        	emailAddress: custEmailAddress,
        	custPayMode: showPayMode,
        	courierCharges: courierCharges,
        	discount: discount,
        	totalAmt: totalAmt,
        	grandTotalAmt: grandTotalAmt,
        	invoiceNo: invoiceNo,
        	custProdInfo: custProdInfo,
        	refNo: refNo,
        	//vatPer: vatPer
        }
	}).done(function( obj ) {
		/*var psjson=JSON.parse(obj);
		var msg=psjson.message;
		*/
		/*if(msg=="success"){*/
			printPage('billPrint');
			alert("Bill is Generated Successfully");
			window.location.href="/jewel/custBill?ACTION=BS";
			
		/*}
		else{
			alert("Bill is Generated Successfully");
		}*/	
	});
	
};

/*Bill calculation*/
function getTotalamount() {
	var table = document.getElementById("bill");

	rowCount = table.rows.length;
	var sum =parseInt(0);
	for ( var count = 1; count <rowCount; count++) {
		var amount = document.getElementById("amount"+count).innerHTML;
		if(!amount==null || !amount=="")
			sum = sum+parseInt(amount);	
	}
	var prodTotal=document.getElementById("prodTotal");
	prodTotal.innerHTML=Math.round(sum);
	
	/*var vat=parseInt(sum)/100;
	sum=parseInt(sum)+parseInt(vat);
	*//*var vatPer=document.getElementById("vatPer");
	vatPer.innerHTML=Math.round(vat);
	*/
	var discount=document.getElementById("discount").value;
	var discountSum=parseInt(sum)*parseInt(discount)/100;
	if(!discountSum==null || !discountSum=="")
		sum=parseInt(sum)-parseInt(discountSum);
	
	var courierCharges=document.getElementById("courierCharges").value;
	if(!courierCharges==null || !courierCharges=="")
		sum=parseInt(sum)+parseInt(courierCharges);
	
	var grandTotal=document.getElementById("grandTotal");
	grandTotal.innerHTML=Math.round(sum);
	
	return sum;
}

/*Bill calculation*/
function calculateAmount(id,rateId,amtId){
	var quantity=document.getElementById(id).value;
	var rate=document.getElementById(rateId).innerHTML;
	var total=quantity*rate;
	var amt=document.getElementById(amtId);
	amt.innerHTML=total;
	getTotalamount();
}

/*Get the Product Information on Numeric Product Code from Product controller*/
function getProductInfo(prodId,prodName,prodPrice){
	var action="SP";
	var scanProdCode=document.getElementById(prodId).value;
	jQuery.ajax({
        type:"post",
        url:"productEntry",
        data:{
        	ACTION: action,
        	scanProdCode: scanProdCode
        }
	}).done(function( obj ) {
		var psjson=JSON.parse(obj);
		var getId=document.getElementById(prodName);
		var jProdName=psjson.name;
		if(jProdName==undefined)
			getId.innerHTML ="NA" ;	
		else
			getId.innerHTML =jProdName ;
		
		var getId2=document.getElementById(prodPrice);
		var jProdPrice=psjson.price;
		if(jProdPrice==undefined)
			getId2.innerHTML ="NA" ;	
		else
			getId2.innerHTML =jProdPrice ;
    });
}

/*Calculate the other charges on bill*/
function otherCharges(){
	
	var sum=document.getElementById("prodTotal").innerHTML;
	
	//var vatPer=document.getElementById("vatPer");
	/*var vat=parseInt(sum)/100;
	sum=parseInt(sum)+parseInt(vat);
	*///vatPer.innerHTML=Math.round(vat);
	
	var courierCharges=document.getElementById("courierCharges").value;
	if(!courierCharges==null || !courierCharges=="")
		sum=parseInt(sum)+parseInt(courierCharges);
	
	var discount=document.getElementById("discount").value;
	var discountSum=parseInt(sum)*parseInt(discount)/100;
	if(!discount==null || !discount=="")
		sum=parseInt(sum)-parseInt(discountSum);
	
	var grandTotal=document.getElementById("grandTotal");
	grandTotal.innerHTML=Math.round(sum);
	
}

/*On select the payment mode */
var selectedPayMode="";
function setPayMode(){
	var selectablePayMode=document.getElementById("showPayMode");
	selectedPayMode = selectablePayMode.options[selectablePayMode.selectedIndex].text;
}

/*Display any on class name on bill*/
function showAllBtn(){
	jQuery(".billHideClass").show();
	jQuery("#selectedMode").hide();
		jQuery(".mainbody").show();
	
}


function generatePDF(){
	
	var custName = document.getElementById("custName").value;
	if(custName==null || custName==""){
		alert("Customer Name is not entered.Please Put Custmor Name");
		return;
	}
	var custAddress = document.getElementById("custAddress").value;
	var custMobile = document.getElementById("custMobile").value;
	var custEmailAddress = document.getElementById("custEmailAddress").value;
	var invoiceNo = document.getElementById("invoiceNo").innerHTML;
	var showPayMode = document.getElementById("showPayMode").value;
	var refNo = document.getElementById("refNo").value;
	//var vatPer = document.getElementById("vatPer").innerHTML;
	
	if(showPayMode=='Select Pay Mode'){
		alert("Please Select Payment Mode");
		return;
	}
	var custProdInfo="[";
	
	var table = document.getElementById("bill");
	rowCount = table.rows.length;
	for ( var count = 1; count <rowCount; count++) {
		var prodCode = document.getElementById("prodCode"+count).value;
		var quantity = document.getElementById("quantity"+count).value;
		var couponCode = document.getElementById("couponCode"+count).value;
		if(couponCode==null || couponCode==""){
			couponCode="-";
		}
		
		var prodPrice = document.getElementById("rate"+count).innerHTML;
		var amount = document.getElementById("amount"+count).innerHTML;
		var prodName = document.getElementById("prodName"+count).innerHTML;
		if(prodCode==null || prodCode==""){
			alert("Product Code is not entered.Please Put Product Code ");
			return;
		}else if(quantity==null || quantity==""){
			alert("Product Quantity is not entered.Please Put Product Quantity");
			return;
		}else if (isNaN(quantity)){
		    alert("Product Quantity is not Number.Please Put Product Quantity is Number");
		    return;
		}
		else{
			var prodInfo="{prodCode:C"+prodCode+",prodName:"+prodName+",quantity:"+quantity+",couponCode:"+couponCode+",prodPrice:"+prodPrice+",amount:"+amount+"},";
			custProdInfo=custProdInfo+prodInfo;
		}
	}
	custProdInfo=custProdInfo+"]";
	var totalAmt = document.getElementById("prodTotal").innerHTML;
	var discount = document.getElementById("discount").value;	
	var courierCharges = document.getElementById("courierCharges").value;
	var grandTotalAmt = document.getElementById("grandTotal").innerHTML;
	var action="GPDF";
	jQuery.ajax({
        type:"post",
        url:"/jewel/custPDF",
        data:{
        	ACTION: action,
        	custName: custName,
        	custAddress: custAddress,
        	custMobile: custMobile,
        	emailAddress: custEmailAddress,
        	custPayMode: showPayMode,
        	courierCharges: courierCharges,
        	discount: discount,
        	totalAmt: totalAmt,
        	grandTotalAmt: grandTotalAmt,
        	invoiceNo: invoiceNo,
        	custProdInfo: custProdInfo,
        	refNo: refNo,
        	//vatPer: vatPer
        }
	}).done(function( obj ) {
		alert("PDF is Generated Successfully");
	});
}